﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace NovelSpider
{
    public class myproc : Collector
    {
        public myproc()
        {

        }


        protected override void InitTarget()
        {
            this.SiteName = "17看";
            this.TargetSite.Add(new Uri("http://www.17k.com/common/include/updating_new_books_index_vip.html"));
        }




        public override List<NovelInfo> GetNovelList(Uri targeturi) 
        {
            List<NovelInfo> list = new List<NovelInfo>();
            HttpClient client = new HttpClient();
            client.UriString = targeturi.AbsoluteUri;
            client.Encoding = Encoding.UTF8;
            client.AllowAutoRedirect = true;
            string res = client.GetString();
            if (string.IsNullOrEmpty(res))
                return null;

            Regex r = new Regex(@"<tr>[\s\S]+?<td[\s\S]+?<td class=""td5"">", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.ExplicitCapture);

            MatchCollection mc = r.Matches(res);
            
            for (int i = 0; i < mc.Count; i++)
            {
                r = new Regex(@"(?<=<td class=""td2""><a href=""\/book\/).+?(?=\.html)", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.ExplicitCapture);
                NovelInfo novelinfo = new NovelInfo();

                novelinfo.Tag = r.Match(mc[i].Value).Value;//保存小说ID

                r = new Regex(@"<td class=""td2""><a href=""(.+?)"" target=""_blank"">(.+?)</a></td>", RegexOptions.IgnoreCase | RegexOptions.Multiline);

                novelinfo.NovelUri = new Uri(targeturi.Scheme + "://" + targeturi.Host + r.Match(mc[i].Value).Groups[1].Value);
                novelinfo.Name = r.Match(mc[i].Value).Groups[2].Value;
                list.Add(novelinfo);
                
            }
            return list;
        }


        public override NovelInfo GetNovelInfo(Uri novelinfouri)
        {
            NovelInfo novelinfo = new NovelInfo();

            HttpClient client = new HttpClient();
            client.UriString = novelinfouri.AbsoluteUri;
            client.Encoding = Encoding.UTF8;
            client.AllowAutoRedirect = true;
            string res = client.GetString();

            if (string.IsNullOrEmpty(res))
                return null;

            Regex r = new Regex(@"<h1.+?href=\""(.+?)\"".+?title.+?\""><font.+?\>(.+?)</font></a></h1>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            Match m = r.Match(res);
            //书名
            if (m.Success)
            {
                novelinfo.Name = m.Groups[2].Value;
                novelinfo.NovelChapterListUri = new Uri(novelinfouri.Scheme + "://" +novelinfouri.Host + m.Groups[1].Value);
            }
            else
            {
                novelinfo.Name = "未知";
                novelinfo.NovelChapterListUri = null;
            }


            r = new Regex(@"(?<=<span>作者：.+?\""><font.+?\>).+?(?=</font></a>)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            m = r.Match(res);
            //作者
            if (m.Success)
                novelinfo.Author = new Author(m.Value);
            else
                novelinfo.Author = new Author("未知"); ;

            r = new Regex(@"(?<=<div class=""c6"" id=""tab91_div0""[\s\S]+?target.+?\""><font.+?\"">)[\s\S]+?(?=</font></a></p>)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            m = r.Match(res);
            //摘要
            if (m.Success)
                novelinfo.Summary = m.Value;
            else
                novelinfo.Summary = "未知";


            //写作进度
            r = new Regex(@"<div class=""bookZt"">[\s\S]+?<div class=""bookFx"">", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.ExplicitCapture);
            m = r.Match(res);
            if (m.Success)
            {

                if (m.Value.IndexOf("目前仍在拼命写作中") > 0)
                    novelinfo.Status = NovelInfo.EnumNovelStatus.WRITTING;
                if (m.Value.IndexOf("此书已完成") > 0)
                    novelinfo.Status = NovelInfo.EnumNovelStatus.FINISHED;
                r = new Regex(@"(?<=已写<em.+>)\d+(?=</em>字)", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.ExplicitCapture);
                novelinfo.Words = Convert.ToInt32(r.Match(m.Value).Value);
            }
            else
                novelinfo.Status = NovelInfo.EnumNovelStatus.UNKNOWN;

            //小说封面
            r = new Regex(@"<div id=""printMarkBox_Title"">[\s\S]+?<div class=""voteBtnList"">", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.ExplicitCapture);
            m = r.Match(res);
            if (m.Success)
            {
                r = new Regex(@"(?<=<img[\s\S]+?src=\"").+?(?=\"" alt[\s\S]+?)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                novelinfo.CoverUri = new Uri(r.Match(m.Value).Value);
            }
            return novelinfo;
        }

        public override List<Volume> GetVolumes(Uri novelchapterlisturi)
        {
            List<Volume> list = new List<Volume>();


            return list;
        }




        public override List<Chapter> GetChapterList(Uri novelchapterlisturi)
        {
            List<Chapter> list = new List<Chapter>();

            return list;
        }

        public override Chapter GetChapter(Uri chapteruri)
        {
            Chapter chapter = new Chapter();

            return chapter;
        }
    }
}
